import { $rp } from '@/utils/constants';

const formatDocPath = doc => `/spaces/${doc.product_id}/resources-docs/${doc.id}`;

const formatIssuePath = issue => `/${$rp.SPACES}/${issue.product_id}/${$rp.SPACE_ISSUES}/${issue.id}`;

export const formatItem = item => ({
  id: item.id,
  type: item.type,
  key: `${item.type}-${item.id}`,
  title: item.title,
  tags: item.tags,
  path: item.type ? formatDocPath(item) : formatIssuePath(item),

  spaceTitle: item.product_title,
  spacePath: `/${$rp.SPACES}/${item.product_id}`,

  authorNickname: item.owner.nickname,
  authorPath: `/${$rp.USERS}/${item.owner.id}`,

  voteLike: item.votes.like_size,

  comments: item.comment_count,

  updated_at: item.updated_at,

  signType: item.type ? 'success' : 'warning',
  signLabel: item.type ? '资源' : '问题',
});

export const formatList = list => list
  .map(item => formatItem(item))
  .sort((a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime());
